﻿Imports Exp.DATA_Service
Imports Exp.Model
Imports Exp.DATA_Service.Exp.DATA_Service
Imports System.Web.UI.WebControls
Public Class Units
    Inherits BasePage
    Const MENUNAME As String = "UNITS"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Session("USER") Is Nothing Then
                Response.Redirect("~/login.aspx")
            Else
                Dim objUser As REM_User = CType(Session("USER"), REM_User)

                Dim objUSV As New Exp.DATA_Service.User_Service
                If Not objUSV.isHasRight(objUser.User_ID, MENUNAME) Then
                    Session("USER") = Nothing
                    Session.Clear()
                    Response.Redirect("~/login.aspx")
                End If
            End If

            gv_data.PageSize = ConfigurationManager.AppSettings("GRID.PAGESIZE")

            BindData()
            BindConTrol()
            BindConTrolSearch()

        End If
    End Sub
#Region "Event"
    Private Sub ddl_Brand_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddl_Brand.SelectedIndexChanged
        BindResort(ddl_Brand.SelectedValue)

    End Sub

    Private Sub btn_Save_Click(sender As Object, e As EventArgs) Handles btn_Save.Click
        Dim sError As String = ""

        If HDFieldUnitID.Value = "" Then
            If CheckInsert(sError) Then
                Save()
                BindData()
                MultiView1.ActiveViewIndex = 0

            End If
        Else
            If CheckInsert(sError) Then
                Edit()
                BindData()
                MultiView1.ActiveViewIndex = 0
            End If
        End If
        If sError.Trim <> "" Then
            JavaScript.AlertMessage(Me.Page, sError)
        End If
        HDFieldUnitID.Value = ""
    End Sub
    Private Sub btn_Add_Click(sender As Object, e As EventArgs) Handles btn_Add.Click
        HDFieldUnitID.Value = ""
        TxtUnitNumber.Text = ""
        txtAddress.Text = ""
        txtDescription.Text = ""
        txtRate.Text = ""
        txtBookNight.Text = ""
        txtAreasize.Text = ""
    End Sub
    Private Sub btn_Cancel_Click(sender As Object, e As EventArgs) Handles btn_Cancel.Click
        HDFieldUnitID.Value = ""
        BindData()
    End Sub
    Protected Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btn_search.Click
        BindDataSearch()
    End Sub
    Protected Sub btnBackView_Click(sender As Object, e As EventArgs) Handles btnBackView.Click
        MultiView1.ActiveViewIndex = 0
        BindData()
    End Sub
#End Region
#Region "Function"
    Sub Save()
        Dim objUser As REM_User = CType(Session("USER"), REM_User)
        Dim Unit_Service As New Unit_Service
        Dim objREM_Unit As New REM_Unit
        If TxtUnitNumber.Text.Trim <> "" Then
            If CheckSaveExisting(0, TxtUnitNumber.Text.Trim) Then
                JavaScript.AlertMessage(Me.Page, "Data existing, please try again")
                Return
            End If
            objREM_Unit.UnitName = TxtUnitNumber.Text
            objREM_Unit.Resort_ID = ddl_Resort.SelectedValue
            objREM_Unit.UnitType_ID = ddl_UnitType.SelectedValue
            objREM_Unit.RatePrice = txtRate.Text.Replace(",", "")
            objREM_Unit.Sqm = txtAreasize.Text
            objREM_Unit.PointPerNight = txtBookNight.Text.Replace(",", "")
            objREM_Unit.Currency_ID = ddlCurrency.SelectedValue
            objREM_Unit.Address = txtAddress.Text
            objREM_Unit.Description = txtDescription.Text
            objREM_Unit.isActive = ddl_Status.SelectedValue
            objREM_Unit.CreateDate = CType(Now, Date)
            objREM_Unit.UpdateDate = CType(Now, Date)
            objREM_Unit.CreateUser = objUser.User_ID
            objREM_Unit.UpdateUser = objUser.User_ID
            Unit_Service.Add(objREM_Unit)
            JavaScript.AlertMessage(Me.Page, "Save data complete")
        Else
            JavaScript.AlertMessage(Me.Page, "Please insert data, try again")
        End If
    End Sub
    Sub Edit()
        Dim objUser As REM_User = CType(Session("USER"), REM_User)
        Dim Unit_Service As New Unit_Service
        Dim objREM_Unit As New REM_Unit
        If txtAreasize.Text <> "" Then
            If CheckSaveExisting(HDFieldUnitID.Value, TxtUnitNumber.Text.Trim) Then
                JavaScript.AlertMessage(Me.Page, "Data existing, please try again")
                Return
            End If
            objREM_Unit.Unit_ID = HDFieldUnitID.Value
            objREM_Unit.UnitName = TxtUnitNumber.Text
            objREM_Unit.Resort_ID = ddl_Resort.SelectedValue
            objREM_Unit.UnitType_ID = ddl_UnitType.SelectedValue
            objREM_Unit.RatePrice = txtRate.Text.Replace(",", "")
            objREM_Unit.Sqm = txtAreasize.Text
            objREM_Unit.PointPerNight = txtBookNight.Text.Replace(",", "")
            objREM_Unit.Currency_ID = ddlCurrency.SelectedValue
            objREM_Unit.Address = txtAddress.Text
            objREM_Unit.Description = txtDescription.Text
            objREM_Unit.isActive = ddl_Status.SelectedValue
            'objREM_Unit.CreateDate = CType(Now, Date)
            objREM_Unit.UpdateDate = CType(Now, Date)
            'objREM_Unit.CreateUser = objUser.User_ID
            objREM_Unit.UpdateUser = objUser.User_ID
            Unit_Service.Update(objREM_Unit)
            JavaScript.AlertMessage(Me.Page, "Save data complete")
        Else
            JavaScript.AlertMessage(Me.Page, "Please insert data, try again")
        End If
    End Sub
    Sub BindData()
        Dim Unit_Service As New Unit_Service
        'Dim lEXP_Select_REM_Unit_And_REM_Resort_Result As New List(Of EXP_Select_REM_Unit_And_REM_Resort_Result)
        'lEXP_Select_REM_Unit_And_REM_Resort_Result = Unit_Service.GetAllData()
        Dim r = Unit_Service.GetAllData().ToList()
        gv_data.DataSource = r
        gv_data.DataBind()
    End Sub
    Sub BindDataSearch()
        Dim Unit_Service As New Unit_Service
        'Dim lEXP_Select_REM_Unit_And_REM_Resort_Result As New List(Of EXP_Select_REM_Unit_And_REM_Resort_Result)
        'lEXP_Select_REM_Unit_And_REM_Resort_Result = Unit_Service.GetAllData()
        Dim lstResult = Unit_Service.GetAllData().ToList()
        If ddlBrandSearch.SelectedValue <> "" Then
            lstResult = lstResult.Where(Function(p) p.Brand_ID = ddlBrandSearch.SelectedValue).ToList()
        End If
        If ddlResortSearch.SelectedValue <> "" Then
            lstResult = lstResult.Where(Function(p) p.Resort_ID = ddlResortSearch.SelectedValue).ToList()
        End If
        If ddlCountrySearch.SelectedValue <> "" Then
            lstResult = lstResult.Where(Function(p) p.Country_ID = ddlCountrySearch.SelectedValue).ToList()
        End If
        If txtLocation.Text.Trim <> "" Then
            lstResult = lstResult.Where(Function(p) Not p.Address Is Nothing).ToList()
            lstResult = lstResult.Where(Function(p) p.Address.ToUpper.Contains(txtLocation.Text.Trim.ToUpper)).ToList()
        End If

        gv_data.DataSource = lstResult
        gv_data.DataBind()
    End Sub
    Sub BindConTrol()
        Dim iBrandID As Integer
        Dim iResortID As Integer
        BindBrand()
        iBrandID = ddl_Brand.SelectedValue
        BindResort(iBrandID)
        iResortID = ddl_Status.SelectedValue
        BindCurrency()
        BindUnitType()
    End Sub
    Sub BindConTrolSearch()
        BindBrandSearch()
        BindResortSearch()
        BindCountrySearch()
    End Sub
    Sub BindBrand()
        Dim REM_Brand As New List(Of REM_Brand)
        Dim Brand_Service As New Brand_Service
        REM_Brand = Brand_Service.GetAll()
        REM_Brand.Distinct()
        ddl_Brand.DataSource = REM_Brand
        ddl_Brand.DataTextField = "BrandName"
        ddl_Brand.DataValueField = "Brand_ID"
        ddl_Brand.DataBind()
    End Sub
    Sub BindResort(Optional ByVal ibrandID As Integer = 0)
        Dim REM_Resort As New List(Of REM_Resort)
        Dim Resort_Service As New Resort_service
        REM_Resort = Resort_Service.GetAll()
        REM_Resort = REM_Resort.Where(Function(p) p.Brand_ID = iBrandID).ToList()
        REM_Resort.Distinct()
        ddl_Resort.DataSource = REM_Resort
        ddl_Resort.DataTextField = "ResortName"
        ddl_Resort.DataValueField = "Resort_ID"
        ddl_Resort.DataBind()
    End Sub
    Sub BindCurrency()
        Dim REM_Currency As New List(Of REM_Currency)
        Dim Currency_Service As New Currency_Service
        REM_Currency = Currency_Service.GetAll()
        REM_Currency.Distinct()
        ddlCurrency.DataSource = REM_Currency
        ddlCurrency.DataTextField = "Currency_Name"
        ddlCurrency.DataValueField = "Currency_ID"
        ddlCurrency.DataBind()
        ddlCurrency.SelectedValue = 136
    End Sub
    Sub BindUnitType()
        Dim REM_UnitType As New List(Of REM_UnitType)
        Dim UnitType_Service As New UnitType_service
        REM_UnitType = UnitType_Service.GetAll()
        'REM_UnitType = REM_UnitType.Where(Function(p) p.UnitType_ID = ibrandID).ToList()
        REM_UnitType.Distinct()
        ddl_UnitType.DataSource = REM_UnitType
        ddl_UnitType.DataTextField = "UnitTypeName"
        ddl_UnitType.DataValueField = "UnitType_ID"
        ddl_UnitType.DataBind()
    End Sub
    Sub BindBrandSearch()
        Dim REM_Brand As New List(Of REM_Brand)
        Dim Brand_Service As New Brand_Service
        REM_Brand = Brand_Service.GetAll()
        REM_Brand.Distinct()
        ddlBrandSearch.DataSource = REM_Brand
        ddlBrandSearch.DataTextField = "BrandName"
        ddlBrandSearch.DataValueField = "Brand_ID"
        ddlBrandSearch.DataBind()
        ddlBrandSearch.Items.Insert(0, New ListItem("", ""))
    End Sub
    Sub BindResortSearch()
        Dim REM_Resort As New List(Of REM_Resort)
        Dim Resort_Service As New Resort_service
        REM_Resort = Resort_Service.GetAll()
        REM_Resort.Distinct()
        ddlResortSearch.DataSource = REM_Resort
        ddlResortSearch.DataTextField = "ResortName"
        ddlResortSearch.DataValueField = "Resort_ID"
        ddlResortSearch.DataBind()
        ddlResortSearch.Items.Insert(0, New ListItem("", ""))
    End Sub
    Sub BindCountrySearch()
        Dim REM_Country As New List(Of REM_Country)
        Dim Country_Service As New Country_Service
        REM_Country = Country_Service.GetAll()
        REM_Country.Distinct()
        ddlCountrySearch.DataSource = REM_Country
        ddlCountrySearch.DataTextField = "CountryName"
        ddlCountrySearch.DataValueField = "Country_ID"
        ddlCountrySearch.DataBind()
        ddlCountrySearch.Items.Insert(0, New ListItem("", ""))
    End Sub
    Function CheckSaveExisting(ByVal iUnitID As Integer, ByVal sUnitName As String) As Boolean
        Dim Unit_Service As New Unit_Service
        Dim LobjREM_Unit As New List(Of REM_Unit)

        LobjREM_Unit = Unit_Service.GetByName(sUnitName)
        If LobjREM_Unit Is Nothing Then
            Return False
        Else
            If LobjREM_Unit.Count > 0 Then
                If LobjREM_Unit.Item(0).Unit_ID = iUnitID Then
                    Return False
                End If
                Return True
            End If
        End If
        Return False
    End Function
    Function CheckInsert(ByRef sError As String) As Boolean
        CheckInsert = True
        If TxtUnitNumber.Text.Trim = "" Then
            sError = "Please insert unit name,"
            'ElseIf CInt(txtAreasize.Text) > 365 Then
            '    sError &= "Quota over 365 day,"
        End If
        If Not IsNumeric(txtAreasize.Text.Trim) Then
            sError &= "Area size error,"
        End If
        If Not IsNumeric(txtRate.Text.Trim) Then
            sError &= "Rate error,"
        End If
        If Not IsNumeric(txtBookNight.Text.Trim) Then
            sError &= "Book night error,"
        End If
        If sError.Length > 1 Then
            CheckInsert = False
            sError = sError.Substring(0, sError.Length - 1)
        End If
    End Function
#End Region

#Region "gv_Data"

    Private Sub gv_data_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles gv_data.PageIndexChanging
        Try

            gv_data.PageIndex = e.NewPageIndex

            BindDataSearch()

        Catch ex As Exception

        End Try
    End Sub
    Private Sub gv_data_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles gv_data.RowCommand
        Dim commandArgs As String = e.CommandArgument.ToString
        Dim iUnitID As Integer = commandArgs
        HDFieldUnitID.Value = iUnitID
        If e.CommandName.ToLower = "edit" Then
            Dim lREM_Unit As New List(Of REM_Unit)
            Dim Unit_Service As New Unit_Service
            lREM_Unit = Unit_Service.GetAll().ToList()
            lREM_Unit = lREM_Unit.Where(Function(p) p.Unit_ID = iUnitID).ToList()
            If Not lREM_Unit Is Nothing Then
                BindConTrol()
                If lREM_Unit.Item(0).Unit_ID > 0 Then
                    TxtUnitNumber.Text = lREM_Unit.Item(0).UnitName
                    txtAddress.Text = lREM_Unit.Item(0).Address
                    txtDescription.Text = lREM_Unit.Item(0).Description
                    txtBookNight.Text = lREM_Unit.Item(0).PointPerNight
                    txtRate.Text = lREM_Unit.Item(0).RatePrice
                    txtAreasize.Text = lREM_Unit.Item(0).Sqm

                    If (lREM_Unit.Item(0).isActive) Then
                        ddl_Status.SelectedValue = 1
                    Else
                        ddl_Status.SelectedValue = 0
                    End If
                    Dim REM_Resort As New REM_Resort
                    Dim Resort_Service As New Resort_service
                    REM_Resort = Resort_Service.GetByID(lREM_Unit.Item(0).Resort_ID)
                    ddl_Brand.SelectedValue = REM_Resort.Brand_ID
                    BindResort(REM_Resort.Brand_ID)
                    ddl_Resort.SelectedValue = lREM_Unit.Item(0).Resort_ID
                    ddl_UnitType.SelectedValue = lREM_Unit.Item(0).UnitType_ID
                    ddlCurrency.SelectedValue = lREM_Unit.Item(0).Currency_ID
                    'ddl_Brand.Enabled = False
                    'ddl_Status.Enabled = False
                    'ddl_Resort.Enabled = False

                    MultiView1.ActiveViewIndex = 1

                End If
            End If
        ElseIf e.CommandName.ToLower = "delete" Then
            Dim Unit_Service As New Unit_Service
            Unit_Service.Delete(iUnitID)
            BindData()
        ElseIf e.CommandName.ToLower = "detail" Then
            ucUnitView.UnitID = iUnitID
            ucUnitView.BindData()
            MultiView1.ActiveViewIndex = 2
        End If
    End Sub
    Private Sub gv_Data_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gv_data.RowDataBound
        Try
            If e.Row.RowType = DataControlRowType.DataRow Then
                Dim UnitType_Service As New UnitType_service
                Dim Country_Service As New Country_Service
                Dim Brand_Service As New Brand_Service
                Dim Resort_Service As New Resort_service

                e.Row.Cells(5).Text = Resort_Service.GetByID(CType(CType(e.Row.Cells(5).FindControl("lblResort_ID"), Label).Text, Integer)).ResortName
                e.Row.Cells(6).Text = UnitType_Service.GetByID(CType(CType(e.Row.Cells(6).FindControl("lblUnitType_ID"), Label).Text, Integer)).UnitTypeName
                e.Row.Cells(8).Text = Brand_Service.GetByID(CType(CType(e.Row.Cells(8).FindControl("lblBrand_ID"), Label).Text, Integer)).BrandName
                e.Row.Cells(9).Text = Country_Service.GetByID(CType(CType(e.Row.Cells(9).FindControl("lblCountry_ID"), Label).Text, Integer)).CountryName
                e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#99CCFF'")
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle;")

            ElseIf e.Row.RowType = DataControlRowType.Pager Then

                Dim rpt As Repeater = e.Row.Cells(0).FindControl("rptPager")
                Dim pages As New List(Of Integer)
                For i As Integer = 0 To gv_data.PageCount
                    pages.Add(i + 1)
                Next

                rpt.DataSource = pages
                rpt.DataBind()

            End If

        Catch ex As Exception

            ' Page.ClientScript.AlertMessage(Me, "gv_Data_RowDataBound", "gv_Data_RowDataBound >>" & ex.Message)

        End Try
    End Sub
    Private Sub gv_data_RowDeleting(sender As Object, e As GridViewDeleteEventArgs) Handles gv_data.RowDeleting
        BindData()
    End Sub
    Private Sub gv_data_RowEditing(sender As Object, e As GridViewEditEventArgs) Handles gv_data.RowEditing
        BindData()
    End Sub

    Protected Sub rptPager_ItemCommand(source As Object, e As RepeaterCommandEventArgs)
        If e.CommandName = "ChangePage" Then
            gv_data.PageIndex = Convert.ToInt32(e.CommandArgument) - 1
        End If
    End Sub

#End Region

    Protected Sub lnkPage_Click(sender As Object, e As EventArgs)
        Try

            gv_data.PageIndex = CInt(CType(sender, LinkButton).Text) - 1

            BindDataSearch()

        Catch ex As Exception

        End Try
    End Sub
End Class